import 'package:flutter/material.dart';
import 'package:sleep/single/filter/base_filter.dart';
import 'package:sleep/single/point_model.dart';

class WaveBufferModel {
  final int index;
  final List<double> data;
  final List<double> nextdata;
  final List<double> olddata;

  WaveBufferModel(this.index, this.data, this.nextdata, this.olddata);
}

class WaveBufferData {
  List<double> _buffer = [];
  List<double> _oldData = [];
  int _bufferIndex = 0;
  final int overWidth;
  final int bufferWidth;

  final ValueChanged<WaveBufferModel> callBack;
  WaveBufferData(this.overWidth, this.bufferWidth, this.callBack);

  addbuffer(List<double> data) {
    _buffer.addAll(data);
    if (_buffer.length >= (overWidth + bufferWidth)) {
      notifier();
    }
  }

  notifier() {
    final buffer = WaveBufferModel(
        _bufferIndex,
        _buffer.sublist(0, bufferWidth), // 0 ~9
        _buffer.sublist(bufferWidth, overWidth + bufferWidth), //10~19
        _oldData); //0
    callBack(buffer);
    _bufferIndex += bufferWidth; //10
    _oldData = _buffer.sublist(bufferWidth - overWidth, bufferWidth);
    _buffer = _buffer.sublist(bufferWidth);
  }

  clear() {
    _buffer = [];
    _bufferIndex = 0;
  }
}

abstract class BaseAnayze {
  final List<BaseFliter> fliteres;
  final List<BaseFliter> showfliteres;
  BaseAnayze(this.fliteres, this.showfliteres);

  List<double> get points;
  List<WaveModel?> get waves;
  int get currentIndex;
  void insertData(List<double> data);
  void clear();

  List<double> toFilter(List<double> data) {
    List<double> filterData = data;
    for (final filter in fliteres) {
      filterData = filter.fliter(filterData);
    }
    return filterData;
  }

  List<double> toShowFilter(List<double> data) {
    List<double> filterData = data;
    for (final filter in showfliteres) {
      filterData = filter.fliter(filterData);
    }
    return filterData;
  }
}
